rm(list=ls())

library(tidyverse); library(gridExtra); library(ggprism); library(stargazer); library(psych); library(xtable); library(marginaleffects); library(ggplot2)

# load and inspect data
speeches <- read.csv("speeches.csv")
head(speeches)


###
### Figure B.2: Keyword Counts and Frequencies in Speeches and Manifestos
### (a) Parliamentary speeches (1949-2020)
###
speech_crime_plot <- speeches %>% 
  pivot_longer( cols=c(count_right, count_left))
head(speech_crime_plot)
speech_crime_plot <- subset(speech_crime_plot, party!="AfD")
speech_crime_plot$party <- factor(speech_crime_plot$party, levels = c("AfD", "CDU", "SPD", "FDP", "Greens", "Left"))
levels(speech_crime_plot$party)

a.plot <- 
ggplot(speech_crime_plot, aes(year,value,col=name,group=name)) + theme_bw() + 
  facet_wrap(~party, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Absolute count") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x=element_blank(), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

b.plot <- 
  ggplot(speech_crime_plot, aes(year,(value/terms)*1000,col=name,group=name)) + theme_bw() + 
  facet_wrap(~party, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Relative frequency") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x = element_text(size=12), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

pdf("FigB2a.pdf", height=7, width=12)
grid.arrange(a.plot , b.plot, nrow=2)
dev.off()


###

###
### Figure B.3: Restricted Keyword Counts and Frequencies in Speeches and Manifestos
### (a) Parliamentary speeches (1949-2020)
###
speech_crime_plot <- speeches %>% 
  pivot_longer( cols=c(count_right_restricted, count_left_restricted))
head(speech_crime_plot)
speech_crime_plot <- subset(speech_crime_plot, party!="AfD")
speech_crime_plot$party <- factor(speech_crime_plot$party, levels = c("AfD", "CDU", "SPD", "FDP", "Greens", "Left"))
levels(speech_crime_plot$party)

a.plot <- 
  ggplot(speech_crime_plot, aes(year,value,col=name,group=name)) + theme_bw() + 
  facet_wrap(~party, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Absolute count") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x=element_blank(), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

b.plot <- 
  ggplot(speech_crime_plot, aes(year,(value/terms)*1000,col=name,group=name)) + theme_bw() + 
  facet_wrap(~party, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Relative frequency") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x = element_text(size=12), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

pdf("FigB3a.pdf.pdf", height=7, width=12)
grid.arrange(a.plot , b.plot, nrow=2)
dev.off()


###

rm(list=ls())

library(tidyverse); library(gridExtra); library(ggprism); library(stargazer); library(psych); library(xtable); library(marginaleffects); library(ggplot2)

# load and inspect data
manifestos <- read.csv("manifestos.csv")
head(manifestos)

###
### Figure B.2: Keyword Counts and Frequencies in Speeches and Manifestos
### (b) State and federal level manifestos (1975-2024)
###
mani_crime_plot <- aggregate(cbind(count_right, count_left, terms) ~ year + partyname, data=manifestos, FUN=mean, na.rm=TRUE, na.action=NULL)
mani_crime_plot <- mani_crime_plot %>% 
  pivot_longer( cols=c(count_right, count_left))
head(mani_crime_plot)
mani_crime_plot$partyname <- ifelse(mani_crime_plot$partyname=="CDU/CSU", "CDU", mani_crime_plot$partyname)
mani_crime_plot <- subset(mani_crime_plot, partyname!="AFD")
mani_crime_plot$partyname <- factor(mani_crime_plot$partyname, ordered=T, levels=c("CDU", "SPD", "FDP", "Greens", "Left"))

a.plot <- 
  ggplot(mani_crime_plot, aes(year,value,col=name,group=name)) + theme_bw() + 
  facet_wrap(~partyname, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Absolute count") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x=element_blank(), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

b.plot <- 
  ggplot(mani_crime_plot, aes(year,(value/terms)*1000,col=name,group=name)) + theme_bw() + 
  facet_wrap(~partyname, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Relative frequency") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x = element_text(size=12), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

pdf("FigB2b.pdf.pdf", height=7, width=12)
grid.arrange(a.plot , b.plot, nrow=2)
dev.off()


###

###
### Figure B.3: Keyword Counts and Frequencies in Speeches and Manifestos
### (b) State and federal level manifestos (1975-2024)
###
mani_crime_plot <- aggregate(cbind(count_right_restricted, count_left_restricted, terms) ~ year + partyname, data=manifestos, FUN=mean, na.rm=TRUE, na.action=NULL)
mani_crime_plot <- mani_crime_plot %>% 
  pivot_longer( cols=c(count_right_restricted, count_left_restricted))
head(mani_crime_plot)
mani_crime_plot$partyname <- ifelse(mani_crime_plot$partyname=="CDU/CSU", "CDU", mani_crime_plot$partyname)
mani_crime_plot <- subset(mani_crime_plot, partyname!="AFD")
mani_crime_plot$partyname <- factor(mani_crime_plot$partyname, ordered=T, levels=c("CDU", "SPD", "FDP", "Greens", "Left"))

a.plot <- 
  ggplot(mani_crime_plot, aes(year,value,col=name,group=name)) + theme_bw() + 
  facet_wrap(~partyname, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Absolute count") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x=element_blank(), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

b.plot <- 
  ggplot(mani_crime_plot, aes(year,(value/terms)*1000,col=name,group=name)) + theme_bw() + 
  facet_wrap(~partyname, ncol=5) +  geom_line() +
  scale_color_manual(values=c("firebrick", "dodgerblue")) +
  ylab("Relative frequency") + geom_hline(yintercept=0, linetype="dashed") +
  scale_x_continuous(guide = "prism_minor", 
                     expand = c(0, 0)
  ) +
  theme(axis.text.x = element_text(size=12), axis.title.x=element_blank(), 
        axis.text.y = element_text(size=12), axis.title.y = element_text(size=16),
        prism.ticks.length.x = unit(5, "pt"),#adjust length of minor tick marks
        legend.position="none",#suppress legend,
        strip.text.x = element_text(size=14), #change color and size of facet strip text
        panel.spacing = unit(1, "lines"), #add more space between panels to better fit axis tick labels
        plot.margin = unit(c(.5, .5, .1, .1), "cm"),#adjust spacing between panels
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )

pdf("FigB3b.pdf", height=7, width=12)
grid.arrange(a.plot , b.plot, nrow=2)
dev.off()